package com.dream.xiaobo.controller;

import com.dream.xiaobo.entity.*;
import com.dream.xiaobo.service.impl.AddressServiceImpl;
import com.dream.xiaobo.service.impl.CartServiceImpl;
import com.dream.xiaobo.service.impl.OrdersServiceImpl;
import com.dream.xiaobo.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

/**
 * @author xiaobo
 * @date 2021/9/2 - 14:55
 */
@WebServlet("/order")
public class OrdersController extends BaseServlet {

    //创建订单业务逻辑实例化对象
    private OrdersServiceImpl ordersService = new OrdersServiceImpl();
    //创建地址业务逻辑实例化对象
    private AddressServiceImpl addressService = new AddressServiceImpl();
    //创建购物车业务逻辑实例化对象
    private CartServiceImpl cartService = new CartServiceImpl();

    /**
     * 订单预览模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //获取用户Id
        String uId = request.getParameter("uid");

        //调用地址业务逻辑方法
        List<Address> addressList = addressService.findAddressByUId(Integer.parseInt(uId));
        //调用购物车业务逻辑方法
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uId));
        //将地址信息返回前端
        request.setAttribute("addressList",addressList);
        //将购物车信息返回前端
        request.setAttribute("cartList",cartList);

        //返回订单预览页面
        return Constants.FOR_WARD + "/order.jsp";
    }

    /**
     * 添加订单模块
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String createOrder(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //获取用户Id
        String uId = request.getParameter("uid");
        //获取总金额
        String sum = request.getParameter("sum");
        //获取地址Id
        String aId = request.getParameter("aid");
        //调用新建订单业务逻辑
        ordersService.createOrder(uId,sum,aId);
        //返回订单显示页面
        return Constants.FOR_WARD + "/order?method=show";
    }

    /**
     * 显示订单模块
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws ParseException
     */
    public String show(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException, ParseException {

        //从Session中取出用户登录信息
        User user = (User) request.getSession().getAttribute("loginUser");

        //判断用户是否存在
        if (user == null) {
            //返回提示信息
            request.setAttribute("msg","查看订单请先登录");
            //返回登录页面
            return  Constants.REDIRECT + "/login.jsp";
        }

        //获取用户Id
        Integer uId = user.getuId();
        //返回订单业务逻辑
        List<Orders> ordersList = ordersService.findOrderByUId(uId);
        //将信息返回到前端
        request.setAttribute("orderList",ordersList);

        //返回订单显示页面
        return Constants.FOR_WARD + "/orderList.jsp";
    }

    /**
     * 显示订单详情模块
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String detail(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //获取订单Id
        String oId = request.getParameter("oid");
        //调用订单业务逻辑方法
        Orders orders = ordersService.findOrderByOId(oId);
        //将查询到信息返回到前端
        request.setAttribute("order",orders);
        //返回订单详情页面
        return Constants.FOR_WARD + "/orderDetail.jsp";
    }

    /**
     * 支付宝支付模块
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String aliPay(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //获取订单Id
        String oId = request.getParameter("oid");
        //调用订单业务逻辑方法
        Orders orders = ordersService.findOrderByOId(oId);
        //将查询到的结果返回到前端
        request.setAttribute("orders",orders);
        //返回到支付宝支付页面
        return Constants.FOR_WARD + "/pay/index.jsp";
    }

    /**
     * 支付宝支付成功模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String aliSuccess(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        //从Session中获取到用户信息
        User user = (User) request.getSession().getAttribute("loginUser");
        //获取到订单Id
        String oId = request.getParameter("oid");
        //调用订单业务逻辑方法
        ordersService.updateOrderByOId(oId);
        //调用购物车业务逻辑方法
        cartService.clearCart(""+user.getuId());
        //返回到首页
        return Constants.FOR_WARD + "/index.jsp";
    }

    /**
     * 更新订单模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String updateOrder(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        //获取到订单Id
        String oId = request.getParameter("oid");
        //调用订单业务逻辑
        ordersService.updateORderByOIdCollect(oId);
        //返回到订单显示页面
        return Constants.FOR_WARD + "/order?method=show";
    }

}
